ENT.Type = "anim";
ENT.Base = "c_base_generator";

ENT.PrintName = "Fusion Generator";
ENT.Information = "Condition: Linked to an coolant container.\nCondition: Linked to an energy container.";

ENT.Model = "models/props_combine/CombineThumper002.mdl";

ENT.Resource = RES_ENERGY;
ENT.Amount = 20;
ENT.Delay = 0.1;

ENT.CosmicPrice = 45;

function ENT:CanGenerate()
	
	return self:AccessResource( RES_ENERGY, 5 );
	
end

function ENT:OnGenerate()
	
	local n = self:GetContainer( RES_ENERGY );
	
	if( n and n:IsValid() ) then
		
		n:RemoveResource( RES_ENERGY, 5 );
		
	end
	
	if( !self.StartNoCoolant or self:AccessResource( RES_COOLANT, 2 ) ) then
		
		self.StartNoCoolant = CurTime();
		
	end
	
	if( self:AccessResource( RES_COOLANT, 2 ) ) then
		
		local n = self:GetContainer( RES_COOLANT );
		
		if( n and n:IsValid() ) then
			
			n:RemoveResource( RES_COOLANT, 2 );
			
		end
		
	end
	
	if( CurTime() > self.StartNoCoolant + 10 ) then
		
		self:Explode();
		
	end
	
end

function ENT:Explode()
	
	local ed = EffectData();
	ed:SetOrigin( self:GetPos() );
	util.Effect( "expl_fusion", ed );
	
	self:EmitSound( Sound( "ambient/explosions/explode_6.wav" ), 511 );
	
	for _, v in pairs( ents.FindInSphere( self:GetPos(), 2048 ) ) do
		
		if( v:IsPlayer() ) then
			
			local dist = v:GetPos() - self:GetPos();
			v:SetVelocity( dist:Normalize() * 10000 );
			v:TakeDamage( 1000, self, self );
			
		end
		
	end
	
	self:Remove();
	
end